//
//  MaintenceDateNewCell.swift
//  koolcar
//
//  Created by xiantian on 2023/10/31.
//

import UIKit
import FSCalendar

class MaintenceDateNewCell: UITableViewCell {
    var bkgView: UIView!
    var calendarView: FSCalendar!
    var showAllBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setup_image(image: UIImage.init(named: "maintence_arrow_down"))
        return tempBtn
    }()
    
//    fileprivate lazy var scopeGesture: UIPanGestureRecognizer = {
//        [unowned self] in
//        let panGesture = UIPanGestureRecognizer(target: self.calendarView, action: #selector(self.calendarView.handleScopeGesture(_:)))
//        panGesture.delegate = self
//        panGesture.minimumNumberOfTouches = 1
//        panGesture.maximumNumberOfTouches = 2
//        return panGesture
//    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        bkgView = UIView()
        bkgView.backgroundColor = Theme.viewBkgGrayColor
        contentView.addSubview(bkgView)
        
        self.calendarView = FSCalendar(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 300))
        self.calendarView.dataSource = self
        self.calendarView.delegate = self
        self.calendarView.scope = .month
        self.calendarView.backgroundColor = .white
        bkgView.addSubview(self.calendarView)

        bkgView.addSubview(showAllBtn)
        let swip = UIPanGestureRecognizer.init(target: self, action: #selector(self.handlePan(pan:)))
        self.showAllBtn.addGestureRecognizer(swip)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        
        bkgView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(0)
            make.height.greaterThanOrEqualTo(300)
            make.bottom.equalTo(0).priority(.high)
        }
        
        self.calendarView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.bottom.equalTo(-10)
        }
        
        showAllBtn.snp.makeConstraints { make in
            make.width.equalTo(24)
            make.height.equalTo(24)
            make.centerX.equalTo(kScreenWidth/2)
            make.centerY.equalTo(calendarView.snp.bottom)
        }
    }
    
    @objc func handlePan(pan: UIPanGestureRecognizer){
        self.calendarView.transitionCoordinator.handleScopeGesture(pan)
        if pan.state == .ended {

        }else{
            // 获取拖拽手势的位置
            let translation = pan.translation(in: self.viewController()?.view!)
//            // 将视图的中心点设置为手势位置加上初始位置
            var tempHeight = self.frame.size.height + translation.y
//            printLog(tempHeight)
            self.bkgView.snp.updateConstraints { make in
                make.height.greaterThanOrEqualTo(tempHeight)
            }
            self.layoutIfNeeded()
            if let table = self.superview as? UITableView {
                table.beginUpdates()
                table.endUpdates()
            }
//            // 重置手势的位置
            pan.setTranslation(CGPoint.zero, in: self.viewController()?.view!)

        }
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
extension MaintenceDateNewCell: FSCalendarDataSource, FSCalendarDelegate {
    
    func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
//        self.calendarHeightConstraint.constant = bounds.height
//        self.view.layoutIfNeeded()
    }
    
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
//        print("did select date \(self.dateFormatter.string(from: date))")
//        let selectedDates = calendar.selectedDates.map({self.dateFormatter.string(from: $0)})
//        print("selected dates is \(selectedDates)")
//        if monthPosition == .next || monthPosition == .previous {
//            calendar.setCurrentPage(date, animated: true)
//        }
    }

    func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
//        print("\(self.dateFormatter.string(from: calendar.currentPage))")
    }
}

//extension MaintenceDateNewCell: UIGestureRecognizerDelegate {
//    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
//        let shouldBegin = false
//        if shouldBegin {
//            let velocity = self.scopeGesture.velocity(in: self)
//            switch self.calendarView.scope {
//            case .month:
//                return velocity.y < 0
//            case .week:
//                return velocity.y > 0
//            }
//        }
//        return shouldBegin
//    }
//}
